package com.qingyun.form.validator

import com.qingyun.form.items.BaseFormItem
import java.util.regex.Pattern

/**
 * 手机号校验器
 */
class PhoneFormValidator : BaseFormValidator() {
    companion object {
        val pattern: Pattern = Pattern.compile(
            "^1(([3,5,8]\\d{9})|(4[5,7]\\d{8})|(7[0,6-8]\\d{8}))$"
        )
    }

    override fun validate(
        item: BaseFormItem,
        value: String?,
    ): FormValidatorResult {
        //不是必填时，不需要校验
        if (!item.required) {
            return success(item)
        }
        val text = value?.trim() ?: ""
        if (pattern.matcher(text).find()) {
            return success(item)
        }
        return error(item, "请输入正确的手机号码")
    }
}